<?
if (!defined('PAGER_CSS_STYLE')) 
	define('PAGER_CSS_STYLE', 'pager');
if (!defined('PAGER_LEFT_ARROW_IMG')) 
	define('PAGER_LEFT_ARROW_IMG', '/public/pager/lewy.gif');
if (!defined('PAGER_RIGHT_ARROW_IMG')) 
	define('PAGER_RIGHT_ARROW_IMG', '/public/pager/prawy.gif');
if (!defined('PAGER_FIRST_ARROW_IMG')) 
	define('PAGER_FIRST_ARROW_IMG', '/public/pager/l_lewy.gif');
if (!defined('PAGER_LAST_ARROW_IMG')) 
	define('PAGER_LAST_ARROW_IMG', '/public/pager/p_prawy.gif');
if (!defined('PAGER_DISABLED_LEFT_ARROW_IMG')) 
	define('PAGER_DISABLED_LEFT_ARROW_IMG', '/public/pager/lewy_gray.gif');
if (!defined('PAGER_DISABLED_RIGHT_ARROW_IMG')) 
	define('PAGER_DISABLED_RIGHT_ARROW_IMG', '/public/pager/prawy_gray.gif');
if (!defined('PAGER_DISABLED_FIRST_ARROW_IMG')) 
	define('PAGER_DISABLED_FIRST_ARROW_IMG', '/public/pager/l_lewy_gray.gif');
if (!defined('PAGER_DISABLED_LAST_ARROW_IMG')) 
	define('PAGER_DISABLED_LAST_ARROW_IMG', '/public/pager/p_prawy_gray.gif');

/**
 * Klasa DataUtils - docelowo zbior statycznych metod obrobki danych
 *
 */
class DataUtils{
	
	/**
	 * Metoda pager - zwraca na podstawie przekazanych parametrow linijke nawigacyjna podzialu na strony - rekordy z bazy
	 *
	 * @param integer $pageIndex - zadany numer strony
	 * @param integer $rCount - liczba rekordow zwrocona przez konkretne zapytanie
	 * @param integer $limit - limit rekordow - do wyliczenia stron
	 * @param string $param - dodatkowe parametry do param, np cmd=costam
	 * @param integer $maxPages - maksymalna ilosc stron wyswietlanych na pasku - najlepiej liczba nieparzysta - srodkowa bedzie dzielic na dwie rowne polowy
	 * @param integer $ajax - 1 jesli output ma uwzgledniac metode javascript serwujaca dane ajaxem, 0 jesli ma byc budowany href=""
	 * @param string $ajaxMethod - nazwa metody ajaxowej do bloku onclick - ostatnim parametrem metody winnien byc indeks strony, pozostale nalezy ujac w $param
	 * @return string
	 */
	public static function pager($pageIndex, $rCount, $limit, $sessionLimit, $sessionOffset, $prefix = '', $suffix = '', $isAjax = 0, $ajaxMethod = 'getPage', $maxPages = 4, $icons = false){
			
		$sessionLimit->limit = $limit;
		if ($rCount > 0){
			$pageNo = ($rCount/$limit);
			if (intval($pageIndex) >= $maxPages){
				$lDelim = intval($pageIndex)-$maxPages;
			}
			else 
				$lDelim = 0;
			
			if ((intval($pageIndex)+$maxPages) < ceil($pageNo) && (ceil($pageNo) > ($maxPages*2))){
				if (intval($pageIndex) < $maxPages){
					$rDelim = (intval($pageIndex)+$maxPages)+($maxPages-(intval($pageIndex))-1);
				}
				else{
					$rDelim = intval($pageIndex)+($maxPages-1);
				}
			}
			else{
				$rDelim = ceil($pageNo);
				if ($pageNo <= ($maxPages*2)) 
					$lDelim = 0;
				else{
					$diff = (intval($pageIndex)+$maxPages) - ceil($pageNo);
					$lDelim = $lDelim-$diff;
				}
			}
					
			if (!$sessionOffset->offset){ 
				$sessionOffset->offset = 0;
			}
				
			if (!$pageIndex) 
				$pageIndex = 1;
				
			if (isset($pageIndex)){
				$sessionOffset->offset = (($limit*intval($pageIndex))-$limit);
			}
	
			//echo $sessionOffset->offset;
			//echo $sessionOffset->limit;
		
			if ($sessionOffset->offset > $rCount) 
				$sessionOffset->offset = ($rCount-$limit);
			if ($sessionOffset->offset < 0) 
				$sessionOffset->offset = 0;
			
			$bottom = ($sessionOffset->offset-$limit);
			$top = ($sessionOffset->offset+$limit);

			$ajaxUrlNext ='';
			$ajaxUrlLast ='';
			$ajaxUrlPrev ='';
			$ajaxUrlFirst ='';
			$ajaxUrl ='';
			$normalUrl ='';
			$normalNext ='';
			$normalPrev ='';
			$normalLast ='';
			$normalFirst ='';

			
			

			if ($top < $rCount){
				
				$ajaxUrlNext = '<a href="#" onclick="'.str_replace('%x%',(intval($pageIndex)+1), $ajaxMethod).'" class="page_no">'.($icons?'<img src="'.PAGER_RIGHT_ARROW_IMG.'" border="0" />':'&gt;').'</a>';
				$normalUrlNext = '<a href="'.$prefix.(intval($pageIndex)+1).$suffix.'" class="page_no">'.($icons?'<img src="'.PAGER_RIGHT_ARROW_IMG.'" border="0" />':'&gt;').'</a>';
				$url = $isAjax==1?$ajaxUrlNext:$normalUrlNext;
				
				$nextPage = '<div class="next_page">'.$url.'</div>';
				
				$ajaxUrlLast = '<a href="#" onclick="'.str_replace('%x%',ceil($pageNo), $ajaxMethod).'" class="page_no">'.($icons?'<img src="'.PAGER_LAST_ARROW_IMG.'" border="0" />':'&gt;&gt;&gt;').'</a>';
				$normalUrlLast = '<a href="'.$prefix.ceil($pageNo).$suffix.'" class="page_no">'.($icons?'<img src="'.PAGER_LAST_ARROW_IMG.'" border="0" />':'&gt;&gt;&gt;').'</a>';
				$url = $isAjax==1?$ajaxUrlLast:$normalUrlLast;
				
				$nextPage .= '<div class="last_page">'.$url.'</div>';
			}else{
				$nextPage = '<div class="next_page_inactive"><a href="#" class="page_no">'.($icons?'<img src="'.PAGER_DISABLED_RIGHT_ARROW_IMG.'" border="0" />':'&gt;').'</a></div>';
				$nextPage .= '<div class="last_page_inactive"><a href="#" class="page_no">'.($icons?'<img src="'.PAGER_DISABLED_LAST_ARROW_IMG.'" border="0" />':'&gt;&gt;&gt;').'</a></div>';
			}

			if ($bottom >= 0){
				$ajaxUrlFirst = '<a href="#" onclick="'.str_replace('%x%','1', $ajaxMethod).'" class="page_no">'.($icons?'<img src="'.PAGER_FIRST_ARROW_IMG.'" border="0" />':'&lt;&lt;&lt;').'</a>';
				$normalUrlFirst = '<a href="'.$prefix.'1'.$suffix.'" class="page_no">'.($icons?'<img src="'.PAGER_FIRST_ARROW_IMG.'" border="0" />':'&lt;&lt;&lt;').'</a>';
				$url = $isAjax==1?$ajaxUrlFirst:$normalUrlFirst;
				
				$prevPage = '<div class="first_page">'.$url.'</div>';

				$ajaxUrlPrev = '<a href="#" onclick="'.str_replace('%x%',(intval($pageIndex)-1), $ajaxMethod).'" class="page_no">'.($icons?'<img src="'.PAGER_LEFT_ARROW_IMG.'" border="0" />':'&lt;').'</a>';
				$normalUrlPrev = '<a href="'.$prefix.(intval($pageIndex)-1).$suffix.'" class="page_no">'.($icons?'<img src="'.PAGER_LEFT_ARROW_IMG.'" border="0" />':'&lt;').'</a>';
				$url = $isAjax==1?$ajaxUrlPrev:$normalUrlPrev;
				
				$prevPage .= '<div class="prev_page">'.$url.'</div>';
			}else{
				
					$prevPage = '<div class="first_page_inactive"><a href="#" class="page_no"> '.($icons?'<img src="'.PAGER_DISABLED_FIRST_ARROW_IMG.'" border="0" />':'&lt;&lt;&lt;').'</a></div>';
					$prevPage .= '<div class="prev_page_inactive"><a href="#" class="page_no">'.($icons?'<img src="'.PAGER_DISABLED_LEFT_ARROW_IMG.'" border="0" />':'&lt;').'</a></div>';
			}
				
			$pages = '<div id="pager">'.$prevPage.'';
				
			$lp = $lDelim;
				
			for ($i = $lDelim; $i < $rDelim; $i++){
				$lp++;
				//$ajaxMethod = str_replace('%x%', $lp, $ajaxMethod);
				$ajaxUrl = '';
				$normalUrl = '';
				if ($lp == intval($pageIndex)) 
					$lp_out = '<a href="#" class="active">'.$lp.'</a>';
				else {
					$ajaxUrl = '<a href="#" onclick="'.str_replace('%x%', $lp, $ajaxMethod).'" class="page_no">'.$lp.'</a>';
					$normalUrl = '<a href="'.$prefix.$lp.$suffix.'" class="page_no">'.$lp.'</a>';
					
					$lp_out = ($isAjax==1?$ajaxUrl:$normalUrl);
				}
				$pages .= '<div>'.$lp_out.'</div>';
			}
				
			$pages .= ''.$nextPage.'</div><div style="clear: both;"></div>';
			
			return $pages;
		}
		else 
			return false;
	}
	
	public static function tfCast($val) {
		return $val=='t' ? 1 : ($val=='f' ? 0 : null);
	}
	
	public static function genThumb($width = 150, $height = 150, $path, $class = null, $ext = null, $method ='zc'){
		$image = '<img src="/minigen.php?filepath='.$path.'&filename=&width='.$width.'&height='.$height.'&ext='.$ext.'&method='.$method.'" />';
		return $image;
	}
	
	public static function ceiling($number, $significance = 1){
        return ( is_numeric($number) && is_numeric($significance) ) ? (ceil($number/$significance)*$significance) : false;
    }
    
    public static function randomString($length = 8, $charsNo = 4){
    	$passGen = new PasswordGenerator($length, $charsNo);
		return $passGen->generate();
    }
}

class PasswordGenerator
{
	var $intLength;
	var $pool;
	
	function __construct($iLength, $iChars)
	{
		$this->intLength = $iLength;
		$this->pool = $this->getPool($iChars);
	}
	
	function getPool($iChars)
	{
		$str = '';
		switch($iChars)
		{
			
			case 1: /* a - z */
				for($i = 0x61; $i <= 0x7A; $i++)
				{
					$str .= chr($i);
				}
				return $str;
				break;
			case 2: /* A - Z */
				for($i = 0x41; $i <= 0x5A; $i++)
				{
					$str .= chr($i);
				}
				return $str;
				break;
			case 3: /* a - z and A - Z */
				$str = $this->getPool(1);
				$str .= $this->getPool(2);
				return $str;
				break;
			case 4: /* 0 - 9, A - Z and a - z */
			    $str = $this->getPool(3); // get chars a - z and A - Z first
				for($i = 0x30; $i <= 0x39; $i++)
				{
					$str .= chr($i); // add chars 0 - 9;
				}
				return $str;
				break;
			case 5:
				/* This will add these chars into the string !#$%&() */
				$str = $this->getPool(4);
				for($i = 0x21; $i < 0x29; $i++)
				{
					if($i == 0x22 || $i == 0x27) // Exclude characters " and '
					{
						continue;
					}
					$str .= chr($i);
				}
				return $str;
				break;
		}
	}
	
	function generate()
	{
		srand ((double) microtime() * 1000000); 
   		$str = ""; 
   		while(strlen($str)< $this->intLength) 
   		{ 
      		$str.=substr($this->pool,(rand()%(strlen($this->pool))),1); 
   		} 
   		return($str);
   	} 
}

?>